#!/bin/bash
#filename: test.sh 

HOME_PATH=/usr1/EcmaCI/openharmony-master
PRODUCT=rk3568
js_file=$1
abc_file=${js_file: : -3}.abc

$HOME_PATH/prebuilts/build-tools/common/nodejs/node-v12.18.4-linux-x64/bin/node --expose-gc $HOME_PATH/out/$PRODUCT/clang_x64/ark/ark/build/src/index.js $js_file -m

export LD_LIBRARY_PATH=$HOME_PATH/out/$PRODUCT/clang_x64/ark/ark:$HOME_PATH/out/$PRODUCT/clang_x64/ark/ark_js_runtime:$HOME_PATH/out/$PRODUCT/clang_x64/thirdparty/icu:$HOME_PATH/prebuilts/clang/ohos/linux-x86_64/llvm/lib

with_new=""
if [ "$1" == "import5000_new.js" ]; then
    with_new=" with new"
fi

getTiming() {
    start=$1  
    end=$2  
     
    start_s=$(echo $start | cut -d '.' -f 1)  
    start_ns=$(echo $start | cut -d '.' -f 2)  
    end_s=$(echo $end | cut -d '.' -f 1)  
    end_ns=$(echo $end | cut -d '.' -f 2)   
  
    total=$(((10#$end_s - 10#$start_s) * 1000 + (10#$end_ns / 1000000 - 10#$start_ns / 1000000)))
    avg_s=$(echo $total | cut -d '.' -f 1)
    avg=$(($avg_s / 10))
    echo "import 5000 classes$with_new spent time total:$total(ms), avg:$avg(ms)"  
}

run () {
    #time=$(date "+%Y-%m-%d %H:%M:%S").$((`date "+%N"`/1000000))
    #echo $time
    for i in `seq 10`:
    do
    $HOME_PATH/out/$PRODUCT/clang_x64/ark/ark_js_runtime/ark_js_vm --asm-interpreter=true $abc_file
    done 
    #time2=$(date "+%Y-%m-%d %H:%M:%S").$((`date "+%N"`/1000000))
    #echo $time2
}

start=$(date +"%s.%N")
run
end=$(date +"%s.%N")
getTiming $start $end

